//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupHostage.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_freed_msg","_die_msg","_shared_free","_shared_alive","_free_code","_die_code","_evt_idx","_action_id"];
DEFAULT_PARAM(_object,TMF_REGISTER_OBJECT,objNull);
DEFAULT_PARAM(_action,TMF_REGISTER_ACTION,[]);
DEFAULT_PARAM(_instance_id,TMF_REGISTER_INSTANCE,TMF_SERVER_INSTANCE_ID);
if (!isnull _object) then {
	_action_id = [_action] call FFUNC(actionGetID);
	TRACE_1("Action",_action);
	_parameters = [_action] call FFUNC(actionGetParams);

	if (_object isKindOf "Man") then {
		_freed_msg = [_parameters, "FREED_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_shared_free = [_parameters, "SHARED_FREE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_free_code = [_parameters, "FREE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_free = _shared_free call FFUNC(getStringVar);
		_shared_alive = _shared_alive call FFUNC(getStringVar);

		_evt_idx = _object addEventHandler ["killed", Format["[_this, %1] call %2;",_action_id,QUOTE(FFUNC(eventObjectiveHostageFailed))]];
		[_object,_action,"HOSTAGE_KILLED_EVENT",_evt_idx] call FFUNC(actionSetLocal);
		_object setCaptive true;
		_object disableAI "AUTOTARGET";
		_object disableAI "MOVE";
		
		_obj_var = format["mission_person_freed_%1",_action_id];
		OSETVARP(_object,_obj_var,false);
		if (!isNil "_shared_free") then {
			if (_shared_free != "") then {
				[_instance_id,_shared_free,false] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_free,false],THIS_FILE] call FFUNC(callEvent);
			};
		};
		if (!isNil "_shared_alive") then {
			if (_shared_alive != "") then {
				[_instance_id,_shared_alive,(alive _object)] spawn FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_alive,(alive _object)],THIS_FILE] call FFUNC(callEvent);
			};
		};
	};
} else {
	ERROR("Object not set");
};
TRACE("End");
TMFPROFILERSTOP;